/**
 * 页面加载完成
 */
+(function(){ 
	var groupVue=new Vue({
		el:"#userGroupContainer",
		data:function(){
			return {
				loading:false,
				loadingTxt:$.admin.loadingTxt,
				tableHeight:pageContentHeight-100,
				tableUserData:[],
				tableUserMultiSelects:[],
				
				currentPage:1,
				defaultPageSize:defaultPageSize,
				pageSize:defaultPageSize,
				totalCount:0,
				qryForm:{
					name:""
				},
				tableData:[],
				tableSelectRow:null,
				
				editDiag:{
					title:"新增用户组",
					type:1,
					form:{
						id:"",
						groupName:"",
						remark:""
					},
					show:false,
					formLabelWidth:"100px",
					rules:{
						groupName:[{ required: true, message: '用户下组名不能为空', trigger: 'blur' }]
					}
				},
				
				groupUserDiag: //用户组关联用户
				{
					title:"关联用户",
					show:false,
					userName:"",
					currentPage:1,
					pageSize:defaultPageSize,
					totalCount:0,
					multiSelects:[]
				},
				diagTableUserData:[],
				
				groupRoleDiag: //用户组关联角色
				{
					title:"关联角色",
					show:false,
					selectRoles:[],
					props:{key:"id",label:"roleName"},
					roles:[],
					groupId:""
				}
			}
		},
		methods:{
			qry:function(page) //查询
			{
				if(1!=page)
				{
					this.currentPage=1;
					this.totalCount=0;
				}
				this.loading=true;
				var url=ctx+"/admin/group/qry";
				var $this=this;
				var params={name:this.qryForm.name,pageIndex:this.currentPage-1,pageSize:this.pageSize};
				$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
					var data=data.data;
					if(data)
					{
						$this.totalCount=data.totalElements;
						$this.tableData=data.content;
						
						$this.$refs["userGroupTable"].setCurrentRow(data.content[0]); //默认选择用户组第一行
					}
					$this.loading=false;
				}});
			},
			openAddPage:function(type,event){  //打开新增/修改页面
				this.editDiag.form;
				this.editDiag.type=type;
				this.editDiag.show=true;
				if(1==type)
				{
					this.editDiag.title="新增用户组";
					this.editDiag.form.id="";
				}
				else if(2==type)
				{
					this.editDiag.title="修改用户组";
					var url=ctx+"/admin/group/one";
					var $this=this;
					var $t=$(event.currentTarget);
					var params={id:$t.attr("pid")};
					$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
						$this.editDiag.form=data.data;
					}});
				}
			},
			save:function(event) //保存
			{
				var $this=this;
				this.$refs["groupDialogForm"].validate(function(valid){
					if(valid==true)
					{
						var msg="保存成功";
						if($this.editDiag.type==1)
						{
							msg="保存成功";
						}
						else
						{
							msg="修改成功";
						}
						var url=ctx+"/admin/group/save";
						var params=$this.editDiag.form;
						$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
							var data=data.data;
							$this.editDiag.show=false;
							$this.resetTable();
							$this.$message({
						          message: msg,
						          type: 'info'
						      });
						}});
					}
				});
			},
			del:function(event)  //删除用户组 
			{
				var $this=this;
				var $t=$(event.currentTarget);
				this.$confirm('确定要删除?', '提示', {
		          confirmButtonText: '确定',
		          cancelButtonText: '取消',
		          type: 'warning'
		        }).then(function(){
		        	var url=ctx+"/admin/group/del";
					var params={id:$t.attr("pid")}
					$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
						if(data.success==true)
						{
							$this.resetTable();
							$this.$message({
					            type: 'info',
					            message: '删除成功！'
					         }); 
						}
						else
						{
							$this.resetTable();
							$this.$message({
					            type: 'warning',
					            message: data.msg
					         });
						}
					}});
		        }).catch(function(){
		          $this.$message({
		            type: 'info',
		            message: '已取消删除'
		          });          
		        });
			},
			
			onCloseEditDialog:function(event) //关闭弹出页面
			{
				this.clearForm();
			},
			clearForm:function() //清理新增表单数据
			{
				var $roleDialogForm =this.$refs["groupDialogForm"];
				if($roleDialogForm)
				{
					$roleDialogForm.resetFields();
				}
			},
			resetTable:function() //重置表格
			{
				this.qry();
			},
			tableSelectChange:function(currentRow, oldCurrentRow)//表格选择改变事件
			{
				if(currentRow)
				{
					this.tableSelectRow=currentRow;
					this.qryUserByGroup();
				}
			},
			handleSizeChange:function(val) {  //分页条改变显示条数
				this.pageSize=val;
				this.qry(1);
			},
	        handleCurrentChange:function(val) { //分页条分页发生改变
	        	this.currentPage=val;
	        	this.qry(1);
	        },
	        //用户组用户
	        qryUserByGroup:function() //根据用户组查询用户
	        {
	        	if(this.tableSelectRow!=null)
	        	{
	        		var groupId=this.tableSelectRow.id;
	        		var url=ctx+"/admin/group/user/qry";
					var $this=this;
					var params={groupId:groupId};
					$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
						var data=data.data;
						if(data)
						{
							$this.tableUserData=data;
						}
						$this.loading=false;
					}});
	        	}
	        },
	        groupUserTableSelectChange:function(val) //用户组关联用户表格选择改变处理
	        {
	        	this.tableUserMultiSelects=val;
	        },
	        openGroupUserWin:function(e){ //打开用户组关联用户窗口
	        	if(this.tableSelectRow==null)
	        	{
	        		this.$message({
			            type: 'info',
			            message: '请选择用户组'
			         });
	        	}
	        	else
	        	{
	        		this.groupUserDiag.show=true;
	        		this.qryGroupRelUser();
	        		
	        	}
	        },
	        groupUserDiagHandleCurrentChange:function(val) //用户组关联用户窗口分页改变处理
	        {
	        	this.groupUserDiag.currentPage=val;
	        	this.qryGroupRelUser(1);
	        },
	        qryGroupRelUser:function(page) //用户组关联用户窗口中可以关联的用户
	        {
	        	if(1!=page) //1表示分页触发,否则重置分页数
	        	{
	        		this.groupUserDiag.currentPage=1;
	        		this.groupUserDiag.totalCount=0;
	        	}
	        	var groupId=this.tableSelectRow.id;
        		var url=ctx+"/admin/group/user/rel";
				var $this=this;
				var params={groupId:groupId,userName:this.groupUserDiag.userName,pageIndex:this.groupUserDiag.currentPage-1,pageSize:this.groupUserDiag.pageSize};
				$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
					var data=data.data;
					if(data)
					{
						$this.diagTableUserData=data.content;
						$this.groupUserDiag.totalCount=data.totalElements
					}
				}});
	        },
	        groupUserDiagTableSelectChange:function(val) //用户组关联用户窗口表格选择改变处理
	        {
	        	this.groupUserDiag.multiSelects=val;
	        },
	        saveGroupUser:function(e) //增加用户组关联用户
	        {
	           // this.$refs["groupUserDiagTable"]
	        	if(this.groupUserDiag.multiSelects!=null && this.groupUserDiag.multiSelects.length>0)
        		{
	        		var url=ctx+"/admin/group/user/save";
					var $this=this;
					var userIds=this.getFieldVals(this.groupUserDiag.multiSelects,"id");
					var params={groupId:this.tableSelectRow.id,userIds:userIds.toString()};
					$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
						if(data.success==true)
						{
							$this.$message({
					            type: 'success',
					            message: '操作成功'
					         });
							$this.groupUserDiag.show=false;
  							$this.qryUserByGroup();
						}
						else
						{
							$this.$message({
					            type: 'warning',
					            message: data.msg
					         });
						}
					}});
        		}
	        	else
	        	{
	        		this.$message({
			            type: 'info',
			            message: '请选择用户'
			         });
	        	}
	        },
	        removeGroupUser:function(e) //移除用户组关联的用户
	        {
	        	var $this=this;
	        	if(this.tableUserMultiSelects!=null && this.tableUserMultiSelects.length>0)
	        	{
	        		this.$confirm('确定要移除?', '提示', {
	  		          confirmButtonText: '确定',
	  		          cancelButtonText: '取消',
	  		          type: 'warning'
	  		        }).then(function(){
	  		        	var url=ctx+"/admin/group/user/del";
						var ids=$this.getFieldVals($this.tableUserMultiSelects,"id");
						var params={ids:ids.toString()};
						$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
	  						if(data.success==true)
	  						{
	  							$this.$message({
	  					            type: 'success',
	  					            message: '移除成功！'
	  					         }); 
	  							$this.groupUserDiag.show=false;
	  							$this.qryUserByGroup();
	  						}
	  						else
	  						{
	  							$this.$message({
	  					            type: 'warning',
	  					            message: data.msg
	  					         });
	  						}
	  					}});
	  		        }).catch(function(){
	  		          $this.$message({
	  		            type: 'info',
	  		            message: '已取消移除'
	  		          });          
	  		        });
	        	}
	        	else
	        	{
	        		this.$message({
			            type: 'info',
			            message: '请选择用户'
			         });
	        	}
	        },
	        onCloseGroupUserDiag:function(e) //关闭用户组关联用户弹窗
	        {
	        	this.groupUserDiag.userName="";
	        	this.diagTableUserData=[];
	        },
	        getFieldVals:function(list,field) //获取值
	        {
	        	var vals=[];
	        	if(list!=null && list.length>0)
	        	{
	        		for(var i=0;i<list.length;i++)
	        		{
	        			vals.push(list[i][field]);
	        		}
	        	}
	        	return vals;
	        },
	        
	        //用户组角色
	        openGroupRoleWin:function(event){ //打开用户组关联角色窗口
	        	this.groupRoleDiag.show=true;
	        	this.groupRoleDiag.groupId=$(event.currentTarget).attr("pid");
	        	this.qryGroupRole();
	        },
	        onCloseGroupRoleDiag:function(e) //关闭用户组关联角色弹窗
	        {
	        	this.groupRoleDiag.roles=[];
	        	this.groupRoleDiag.selectRoles=[];
	        },
	        saveGroupRole:function(e) //保存用户组关联角色
	        {
	        	var url=ctx+"/admin/group/role/save";
				var $this=this;
				var params={groupId:this.groupRoleDiag.groupId,roleIds:this.groupRoleDiag.selectRoles.toString()};
				$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
					if(data.success==true)
					{
						$this.$message({
				            type: 'success',
				            message: '操作成功'
				         });
						$this.groupRoleDiag.show=false;
						$this.qry();
					}
					else
					{
						$this.$message({
				            type: 'warning',
				            message: data.msg
				         });
					}
				}});
	        },
	        qryGroupRole:function() //查询用户组关联角色
	        {
        		var url=ctx+"/admin/group/role/all";
				var $this=this;
				var params={groupId:this.groupRoleDiag.groupId};
				$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
					var data=data.data;
					if(data)
					{
						$this.groupRoleDiag.roles=data.roles;
						$this.groupRoleDiag.selectRoles=data.selectRoles;
					}
				}});
	        }
	        
		}
	});
	groupVue.qry();
})();